package com.dlc.shop.jky.common.foreign.service.impl;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dlc.shop.allinpay.service.AllinpayCompanyService;
import com.dlc.shop.bean.app.param.ShopAuditingParam;
import com.dlc.shop.bean.dto.*;
import com.dlc.shop.bean.model.*;
import com.dlc.shop.bean.param.OfflineHandleEventAuditParam;
import com.dlc.shop.bean.vo.BrandShopVO;
import com.dlc.shop.bean.vo.CategoryShopVO;
import com.dlc.shop.bean.vo.ShopCompanyVO;
import com.dlc.shop.common.enums.SysTypeEnum;
import com.dlc.shop.common.foreign.ipPortMap;
import com.dlc.shop.common.util.BeanUtil;
import com.dlc.shop.common.util.PageParam;
import com.dlc.shop.config.ShopConfig;
import com.dlc.shop.jky.common.foreign.service.ForeignShopService;
import com.dlc.shop.service.*;
import com.dlc.shop.sys.common.model.ShopEmployee;
import com.dlc.shop.sys.common.service.ShopEmployeeService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
@AllArgsConstructor
public class ForeignShopServiceImpl implements ForeignShopService {

    private final ShopDetailService shopDetailService;
    private final AllinpayCompanyService allinpayCompanyService;
    private final ShopAuditingService shopAuditingService;
    private final ShopEmployeeService shopEmployeeService;
    private final ShopBankCardService shopBankCardService;
    private final ShopCompanyService shopCompanyService;
    private final CategoryShopService categoryShopService;
    private final BrandShopService brandShopService;
    private final WarehouseService warehouseService;
    private final ShopConfig shopConfig;

    public String getUrlIpPort(){
        ipPortMap ipPortMap = shopConfig.getForeignUrl();
        return ipPortMap.getUrl();
    }


    @Override
    public String shopCreateInfo(Long shopId) {

        ShopCreateInfoDTO shopCreateInfoDTO = shopCreateInfoSend(shopId);

        Long warehouseId = warehouseService.getDefaultWarehouseByShopId(shopId, SysTypeEnum.MULTISHOP.value()).getWarehouseId();
        shopCreateInfoDTO.setWarehouseId(warehouseId);

        Map<String, String> map = new HashMap<>();
        map.put("shopAuditingParam", "");
        map.put("shopCreateInfoDTO", JSON.toJSONString(shopCreateInfoDTO));

        //调用 https请求 链接进行同步数据
        // 构建body POST请求
        // 请求的URL
        String urlIpPort = getUrlIpPort();
        if (urlIpPort == null || urlIpPort.equals("")){
            return "";
        }
        String url = urlIpPort+ "/synchronize/shop/add";

        // 构建POST请求
        HttpRequest request = HttpRequest.post(url).header("Content-Type", "application/json") // 设置请求头
                .body(JSON.toJSONString(shopCreateInfoDTO)); // 设置请求体，这里是一个JSON字符串

        // 发送请求，并接收响应
        HttpResponse response = request.execute();

        // 打印响应的内容
        log.info(response.body());
        return response.body();
    }


    public ShopCreateInfoDTO shopCreateInfoSend(Long shopId) {
        ShopCreateInfoDTO shopCreateInfoDTO = new ShopCreateInfoDTO();
        //商家详细信息
        ShopDetail shopDetailByShopId = shopDetailService.getShopDetailByShopId(shopId);
        shopCreateInfoDTO.setShopDetail(BeanUtil.map(shopDetailByShopId, ShopDetail.class));

        //商家注册
        List<ShopEmployee> shopEmployeeList = shopEmployeeService.listEnableEmployee(shopId);
        ShopUserRegisterDto shopUserRegisterInfo = new ShopUserRegisterDto();
        for (ShopEmployee shopEmployee : shopEmployeeList) {
            if (shopEmployee.getType() == 0) {
                shopUserRegisterInfo.setShopId(shopId);
                shopUserRegisterInfo.setUsername(shopEmployee.getUsername());
                shopUserRegisterInfo.setMobile(shopEmployee.getMobile());
                shopUserRegisterInfo.setPassword("123456");
            }
        }
        shopCreateInfoDTO.setShopUserRegisterInfo(shopUserRegisterInfo);
        //银行卡信息
        List<ShopBankCard> shopBankCards = shopBankCardService.list(new LambdaQueryWrapper<ShopBankCard>().eq(ShopBankCard::getShopId, shopId).eq(ShopBankCard::getStatus, 1));
        shopCreateInfoDTO.setShopBankCardList(shopBankCards);
        //公司信息
        ShopCompanyVO shopCompanyVo = shopCompanyService.getInfo(shopId, 1);
        shopCreateInfoDTO.setShopCompany(BeanUtil.map(shopCompanyVo, ShopCompany.class));

        //签约分类
        List<CategoryShopVO> list = categoryShopService.listSigningCategoryByShopId(shopId);
        List<CategoryShop> categoryShops = new ArrayList<>();
        for (CategoryShopVO categoryShopVO : list) {
            categoryShops.add(BeanUtil.map(categoryShopVO, CategoryShop.class));
        }
        shopCreateInfoDTO.setCategorySigningList(categoryShops);
        //签约品牌列表
        PageParam<BrandShopVO> pageBrand = new PageParam<>();
        pageBrand.setSize(1);
        pageBrand.setCurrent(1000);
        BrandShopDTO brandShop = new BrandShopDTO();
        brandShop.setShopId(shopId);
        IPage<BrandShopVO> brandShopVOIPage = brandShopService.pageSigningByShopId(pageBrand, brandShop);
        List<BrandShopDTO> brandShopDTOS = new ArrayList<>();
        for (BrandShopVO record : brandShopVOIPage.getRecords()) {
            brandShopDTOS.add(BeanUtil.map(record, BrandShopDTO.class));
        }
        shopCreateInfoDTO.setBrandSigningList(brandShopDTOS);

        return shopCreateInfoDTO;
    }

    @Override
    public String audit(ShopAuditingParam shopAuditingParam) {
        Long shopId = shopAuditingParam.getShopId();
        ShopCreateInfoDTO shopCreateInfoDTO = shopCreateInfoSend(shopId);

        Long warehouseId = warehouseService.getDefaultWarehouseByShopId(shopId, SysTypeEnum.MULTISHOP.value()).getWarehouseId();
        shopCreateInfoDTO.setWarehouseId(warehouseId);


        ShopDetail shopDetail = shopCreateInfoDTO.getShopDetail();
        shopDetail.setContractStartTime(shopAuditingParam.getContractStartTime());
        shopDetail.setContractEndTime(shopAuditingParam.getContractEndTime());
        shopDetail.setIsSupplier(shopAuditingParam.getIsSupplier());
        shopDetail.setType(shopAuditingParam.getShopId().intValue());
        //调用 https请求 链接进行同步数据
        // 构建body POST请求
        // 请求的URL
        String urlIpPort = getUrlIpPort();
        if (urlIpPort == null || urlIpPort.equals("")){
            return "";
        }
        String url = urlIpPort+ "/synchronize/shop/add";

        // 构建POST请求
        HttpRequest request = HttpRequest.post(url).header("Content-Type", "application/json") // 设置请求头
                .body(JSON.toJSONString(shopCreateInfoDTO)); // 设置请求体，这里是一个JSON字符串

        // 发送请求，并接收响应
        HttpResponse response = request.execute();

        // 打印响应的内容
        System.out.println(response.body());
        return response.body();
    }

    /**
     * 平台直接注册的
     * @param shopCreateInfo
     */
    @Override
    public String shopPlatformCreateInfo(ShopCreateInfoDTO shopCreateInfo) {
        //调用 https请求 链接进行同步数据
        // 构建body POST请求
        // 请求的URL
        String urlIpPort = getUrlIpPort();
        if (urlIpPort == null || urlIpPort.equals("")){
            return "";
        }
        String url = urlIpPort+ "/synchronize/shop/add";
        Long shopId = shopCreateInfo.getShopDetail().getShopId();
        Long warehouseId = warehouseService.getDefaultWarehouseByShopId(shopId, SysTypeEnum.MULTISHOP.value()).getWarehouseId();
        ShopDetail shopDetailByShopId = shopDetailService.getShopDetailByShopId(shopId);
        shopCreateInfo.setShopDetail(BeanUtil.map(shopDetailByShopId, ShopDetail.class));
        shopCreateInfo.setWarehouseId(warehouseId);
        ShopUserRegisterDto shopUserRegisterInfo = shopCreateInfo.getShopUserRegisterInfo();
        shopUserRegisterInfo.setUserId(shopDetailByShopId.getUserId());
        shopUserRegisterInfo.setPassword("123456");
        // 构建POST请求
        HttpRequest request = HttpRequest.post(url).header("Content-Type", "application/json") // 设置请求头
                .body(JSON.toJSONString(shopCreateInfo)); // 设置请求体，这里是一个JSON字符串

        // 发送请求，并接收响应
        HttpResponse response = request.execute();

        // 打印响应的内容
       return response.body();
    }

    @Override
    public String offline(OfflineHandleEvent offlineHandleEvent) {
        //调用 https请求 链接进行同步数据
        // 构建body POST请求
        // 请求的URL
        String urlIpPort = getUrlIpPort();
        if (urlIpPort == null || urlIpPort.equals("")){
            return "";
        }
        String url = urlIpPort+ "/synchronize/shop/offline";

        // 构建POST请求
        HttpRequest request = HttpRequest.post(url).header("Content-Type", "application/json") // 设置请求头
                .body(JSON.toJSONString(offlineHandleEvent)); // 设置请求体，这里是一个JSON字符串

        // 发送请求，并接收响应
        HttpResponse response = request.execute();

        // 打印响应的内容
        return response.body();
    }

    @Override
    public String auditShop(OfflineHandleEventAuditParam offlineHandleEventAuditParam) {
        //调用 https请求 链接进行同步数据
        // 构建body POST请求
        // 请求的URL
        String urlIpPort = getUrlIpPort();
        if (urlIpPort == null || urlIpPort.equals("")){
            return "";
        }
        String url = urlIpPort+ "/synchronize/shop/auditShop";

        // 构建POST请求
        HttpRequest request = HttpRequest.post(url).header("Content-Type", "application/json") // 设置请求头
                .body(JSON.toJSONString(offlineHandleEventAuditParam)); // 设置请求体，这里是一个JSON字符串

        // 发送请求，并接收响应
        HttpResponse response = request.execute();

        // 打印响应的内容
        return response.body();
    }

    @Override
    public String onlineOpenShop(OfflineHandleEventAuditParam offlineHandleEventAuditParam) {
        //调用 https请求 链接进行同步数据
        // 构建body POST请求
        // 请求的URL
        String urlIpPort = getUrlIpPort();
        if (urlIpPort == null || urlIpPort.equals("")){
            return "";
        }
        String url = urlIpPort+ "/synchronize/shop/onlineOpenShop";

        // 构建POST请求
        HttpRequest request = HttpRequest.post(url).header("Content-Type", "application/json") // 设置请求头
                .body(JSON.toJSONString(offlineHandleEventAuditParam)); // 设置请求体，这里是一个JSON字符串

        // 发送请求，并接收响应
        HttpResponse response = request.execute();

        // 打印响应的内容
        return response.body();
    }

    @Override
    public String auditApply(OfflineHandleEventAuditParam offlineHandleEventAuditParam) {
        //调用 https请求 链接进行同步数据
        // 构建body POST请求
        // 请求的URL
        String urlIpPort = getUrlIpPort();
        if (urlIpPort == null || urlIpPort.equals("")){
            return "";
        }
        String url = urlIpPort+ "/synchronize/shop/auditApply";

        // 构建POST请求
        HttpRequest request = HttpRequest.post(url).header("Content-Type", "application/json") // 设置请求头
                .body(JSON.toJSONString(offlineHandleEventAuditParam)); // 设置请求体，这里是一个JSON字符串

        // 发送请求，并接收响应
        HttpResponse response = request.execute();

        // 打印响应的内容
        return response.body();
    }

    @Override
    public String editShop(ShopDetail shopDetail) {
        //调用 https请求 链接进行同步数据
        // 构建body POST请求
        // 请求的URL
        String urlIpPort = getUrlIpPort();
        if (urlIpPort == null || urlIpPort.equals("")){
            return "";
        }
        String url = urlIpPort+ "/synchronize/shop/editShop";

        // 构建POST请求
        HttpRequest request = HttpRequest.post(url).header("Content-Type", "application/json") // 设置请求头
                .body(JSON.toJSONString(shopDetail)); // 设置请求体，这里是一个JSON字符串

        // 发送请求，并接收响应
        HttpResponse response = request.execute();

        // 打印响应的内容
        return response.body();
    }

    @Override
    public String updateSigningInfo(ShopSigningInfoDTO shopSigningInfoDTO) {
        //调用 https请求 链接进行同步数据
        // 构建body POST请求
        // 请求的URL
        String urlIpPort = getUrlIpPort();
        if (urlIpPort == null || urlIpPort.equals("")){
            return "";
        }
        String url = urlIpPort+ "/synchronize/shop/updateSigningInfo";

        // 构建POST请求
        HttpRequest request = HttpRequest.post(url).header("Content-Type", "application/json") // 设置请求头
                .body(JSON.toJSONString(shopSigningInfoDTO)); // 设置请求体，这里是一个JSON字符串

        // 发送请求，并接收响应
        HttpResponse response = request.execute();

        // 打印响应的内容
        return response.body();
    }

    @Override
    public String editShopCompany(ShopCompany shopCompany) {
        //调用 https请求 链接进行同步数据
        // 构建body POST请求
        // 请求的URL
        String urlIpPort = getUrlIpPort();
        if (urlIpPort == null || urlIpPort.equals("")){
            return "";
        }
        String url = urlIpPort+ "/synchronize/shop/editShopCompany";

        // 构建POST请求
        HttpRequest request = HttpRequest.post(url).header("Content-Type", "application/json") // 设置请求头
                .body(JSON.toJSONString(shopCompany)); // 设置请求体，这里是一个JSON字符串

        // 发送请求，并接收响应
        HttpResponse response = request.execute();

        // 打印响应的内容
        return response.body();
    }

    @Override
    public String addSigningCategory(List<CategoryShop> categoryShopList) {
        //调用 https请求 链接进行同步数据
        // 构建body POST请求
        // 请求的URL
        String urlIpPort = getUrlIpPort();
        if (urlIpPort == null || urlIpPort.equals("")){
            return "";
        }
        String url = urlIpPort+ "/synchronize/shop/addSigningCategory";

        // 构建POST请求
        HttpRequest request = HttpRequest.post(url).header("Content-Type", "application/json") // 设置请求头
                .body(JSON.toJSONString(categoryShopList)); // 设置请求体，这里是一个JSON字符串

        // 发送请求，并接收响应
        HttpResponse response = request.execute();

        // 打印响应的内容
        return response.body();
    }

    @Override
    public String deleteSigningCategory(Long shopId, Long categoryId) {
        //调用 https请求 链接进行同步数据
        // 构建body POST请求
        // 请求的URL
        String urlIpPort = getUrlIpPort();
        if (urlIpPort == null || urlIpPort.equals("")){
            return "";
        }
        String url = urlIpPort+ "/synchronize/shop/deleteSigningCategory";

        HashMap<Object, Object> map = new HashMap<>();
        map.put("shopId", shopId);
        map.put("categoryId", categoryId);
        // 构建POST请求
        HttpRequest request = HttpRequest.post(url).header("Content-Type", "application/json") // 设置请求头
                .body(JSON.toJSONString(map)); // 设置请求体，这里是一个JSON字符串

        // 发送请求，并接收响应
        HttpResponse response = request.execute();

        // 打印响应的内容
        return response.body();
    }

    @Override
    public String addSigningBrand(BrandSigningDTO brandSigningDTO) {
        //调用 https请求 链接进行同步数据
        // 构建body POST请求
        // 请求的URL
        String urlIpPort = getUrlIpPort();
        if (urlIpPort == null || urlIpPort.equals("")){
            return "";
        }
        String url = urlIpPort+ "/synchronize/shop/addSigningBrand";

        // 构建POST请求
        HttpRequest request = HttpRequest.post(url).header("Content-Type", "application/json") // 设置请求头
                .body(JSON.toJSONString(brandSigningDTO)); // 设置请求体，这里是一个JSON字符串

        // 发送请求，并接收响应
        HttpResponse response = request.execute();

        // 打印响应的内容
        return response.body();
    }

    @Override
    public String deleteSigningBrand(Long brandShopId) {
        //调用 https请求 链接进行同步数据
        // 构建body POST请求
        // 请求的URL
        String urlIpPort = getUrlIpPort();
        if (urlIpPort == null || urlIpPort.equals("")){
            return "";
        }
        String url = urlIpPort+ "/synchronize/shop/deleteSigningBrand";

        HashMap<Object, Object> map = new HashMap<>();
        map.put("brandShopId", brandShopId);
        // 构建POST请求
        HttpRequest request = HttpRequest.post(url).header("Content-Type", "application/json") // 设置请求头
                .body(JSON.toJSONString(map)); // 设置请求体，这里是一个JSON字符串

        // 发送请求，并接收响应
        HttpResponse response = request.execute();

        // 打印响应的内容
        return response.body();
    }

    @Override
    public String updateCategoryRate(Long categoryShopId, Double rate) {
        //调用 https请求 链接进行同步数据
        // 构建body POST请求
        // 请求的URL
        String urlIpPort = getUrlIpPort();
        if (urlIpPort == null || urlIpPort.equals("")){
            return "";
        }
        String url = urlIpPort+ "/synchronize/shop/updateCategoryRate";

        HashMap<Object, Object> map = new HashMap<>();
        map.put("categoryShopId", categoryShopId);
        map.put("rate", rate);
        // 构建POST请求
        HttpRequest request = HttpRequest.post(url).header("Content-Type", "application/json") // 设置请求头
                .body(JSON.toJSONString(map)); // 设置请求体，这里是一个JSON字符串

        // 发送请求，并接收响应
        HttpResponse response = request.execute();

        // 打印响应的内容
        return response.body();
    }

    @Override
    public String updateRate(Long shopId, Long categoryId, Double rate) {
        //调用 https请求 链接进行同步数据
        // 构建body POST请求
        // 请求的URL
        String urlIpPort = getUrlIpPort();
        if (urlIpPort == null || urlIpPort.equals("")){
            return "";
        }
        String url = urlIpPort+ "/synchronize/shop/updateRate";

        HashMap<Object, Object> map = new HashMap<>();
        map.put("shopId", shopId);
        map.put("categoryId", categoryId);
        map.put("rate", rate);
        // 构建POST请求
        //createPost(url)
        //                                      .header("Content-Type", "application/x-www-form-urlencoded")
        //                                      .form(param1, param1Value)
        //                                      .form(param2, param2Value);
        HttpRequest request = HttpRequest.post(url).header("Content-Type", "application/json") // 设置请求头
                .body(JSON.toJSONString(map)); // 设置请求体，这里是一个JSON字符串

        // 发送请求，并接收响应
        HttpResponse response = request.execute();

        // 打印响应的内容
        return response.body();
    }
}
